function SubjectCourseList() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTree();
        _this.initTable();
        _this.initEvent();
    };

    var tree_options = {
        id: 'subject_treeId',
        url: appPath + '/student/studySelf/subjectTree',
        setting: {
            view: {
                dblClickExpand: false,
                fontCss: getFontCss
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    if(treeNode.id == 0){
                        return;
                    }
                    _this.onClick(e, treeId, treeNode);
                }
            }
        },
        zNodes: '',
        rMenu: ''
    };

    //初始化树
    _this.initTree = function(){
        var url = tree_options.url;
        var result = _this.ajax(url);
        tree_options.zNodes = eval(result);
        $.fn.zTree.init($("#subject_treeId"), tree_options.setting, tree_options.zNodes);
        //默认选中第一个节点
        var treeObj = $.fn.zTree.getZTreeObj("subject_treeId");
        var firstNodes = treeObj.getNodesByFilter(function(node){return node.level==1;});
        if(firstNodes.length == 0){
            return;
        }
        var firstNode = firstNodes[0];
        // 默认选第一个节点
        treeObj.selectNode(firstNode);
        $("#subjectId").val(firstNode.id);
    };

    // 初始化课程列表
    _this.initTable = function() {
        var list_url = appPath + "/student/studySelf/subjectCourseList";
        page.init("subject_course_form", list_url, "subject_course_list", "subject_course_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td width="50">' + (i+1) + '</td>';
                inner += '<td  width="100">'+ item['course_no'] + '</td>';
                inner += '<td ><span class="text-orange tooltip" data-length="15">'+ item['course_name'] + '</span></td>';
                inner += '<td width="80">'+ item['class_hour'] + '</td>';
                inner += '<td width="80">'+ item['question_count'] + '</td>';
                inner += '<td width="80">'+ TimeUtil.getHouAndMinAndSec(item['study_time'])+ '</td>';
                inner += '<td width="160"><a href="javascript: course_list.courseView(\''+ item['course_id'] +'\');" class="a a-info">学习</a> ';
                inner += '<a href="javascript:course_list.questionView(\''+ item['course_id'] +'\');" class="a a-publish">练习</a></td>';
                inner += '</tr>';
            }
            return inner;
        }

    };

    // 点击
    _this.onClick = function(e,treeId, treeNode) {
        $("#subjectId").val(treeNode.id);
        _this.initTable();
    };


    _this.initEvent = function () {
        // 搜索
        $("#courseName_search").click(function () {
            $("#courseName").val($.trim($("#courseName").val()));
            _this.initTable();
        });
       // 全部
        $("#course_all").click(function () {
            $("#courseName").val("");
            _this.initTable();
        });

    };

    // 关闭对话框
    _this.closeDialog =function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    };

    // 学习
    _this.courseView=function (courseId) {
        common.goto_student_study(null,courseId,null,$("#subjectId").val());

    };
    // 练习
    _this.questionView=function (courseId) {
        window.open(appPath + "/student/studySelf/exercise?course_id=" + courseId);
    };


    _this.ajax = function(url, param, type){
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data.result;
            }
        });
        return result;
    };

}

var course_list = new SubjectCourseList();


//搜索高亮
function getFontCss(treeId, treeNode){
    return (!!treeNode.highlight)?{'color': "#A60000", 'font-weight':"bold"}:{'color': "#333", 'font-weight':"normal"};
}